package com.etcfg.etlaunch.ui;

import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import com.etcfg.etlaunch.ETLauncherApplication;
import com.etcfg.etlaunch.ETServer;

public class ImportFromHLSWAction extends AbstractAction {

	ETLauncherApplication application;

	public ImportFromHLSWAction(ETLauncherApplication application) {
		this.application = application;
		setEnabled(true);
		putValue(NAME, "Import from hlsw");

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(false);
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setFileFilter(new FileFilter() {

			@Override
			public String getDescription() {
				return "*.sslf";
			}

			@Override
			public boolean accept(File f) {
				return f.isDirectory() || f.getName().endsWith(".sslf");

			}
		});
		int showOpenDialog = chooser.showOpenDialog(null);
		if (showOpenDialog == JFileChooser.APPROVE_OPTION) {
			File selectedFile = chooser.getSelectedFile();
			try {
				FileReader fileReader = new FileReader(selectedFile);
				BufferedReader bufferedReader = new BufferedReader(fileReader);
				String readLine = bufferedReader.readLine();
				while (readLine != null) {

					if (readLine.startsWith("Server=")) {
						readLine = readLine.substring("Server=".length());
						if (readLine.charAt(0) == 'E'
								&& readLine.charAt(1) == 'T') {
							ETServer etServer = new ETServer();

							System.out.println(readLine);
							int i = 3;
							StringBuilder builder = new StringBuilder();
							while (i < readLine.length()) {
								char charAt = readLine.charAt(i);
								if (charAt != ' ') {
									if (charAt != ':') {
										builder.append(charAt);
									} else {
										etServer.setIp(builder.toString());
										System.out.println(builder);

										builder = new StringBuilder();
									}
								} else {
									i++;
									break;
								}
								i++;
							}
							System.out.println(builder);

							etServer.setPort(Integer.parseInt(builder
									.toString()));
							builder = new StringBuilder();
							while (i < readLine.length()) {
								char charAt = readLine.charAt(i);
								if (charAt != ' ') {

									builder.append(charAt);
								} else {
									i++;
									break;
								}
								i++;
							}
							if (builder.toString().equals("") == false) {
								etServer.setPassword(builder.toString());
							}
							builder = new StringBuilder();
							while (i < readLine.length()) {
								builder.append(readLine.charAt(i));
								i++;
							}
							etServer.setServerName(builder.toString());
							try {
								application.addServer(etServer);
							} catch (Exception ex) {
								ex.printStackTrace();
							}
						}
					}
					readLine = bufferedReader.readLine();

				}

			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			}
		}

	}
}
